﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ObjectManager;
using Process;

namespace QuanLyDiem
{
    public partial class frmSinhVien_Sua : Form
    {
        private static frmSinhVien_Sua frm = null;
        private BoMon bomon = new BoMon();
        private Lop lop = new Lop();
        private SinhVien sinhvien = null;

        public event UpdateSinhVien UpdateSuccess = null;

        private frmSinhVien_Sua()
        {
            InitializeComponent();
        }

        private frmSinhVien_Sua(SinhVien sinhvien)
        {
            InitializeComponent();
            this.StartPosition = FormStartPosition.CenterScreen;
            this.sinhvien = sinhvien;
        }

        public static frmSinhVien_Sua Create()
        {
            if (frm == null)
            {
                frm = new frmSinhVien_Sua();
            }
            frm.UpdateData();
            return frm;
        }

        public static frmSinhVien_Sua Create(SinhVien sinhvien)
        {
            if (frm == null)
            {
                frm = new frmSinhVien_Sua(sinhvien);
            }
            frm.sinhvien = sinhvien;
            frm.UpdateData();
            return frm;
        }

        private void frmSinhVien_Sua_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (frm != null)
            {
                frm.Dispose();
                frm = null;
            }
        }

        private void UpdateData()
        {
            txtMaSV.Text = sinhvien.ThongTin.Ma;
            txtTenSV.Text = sinhvien.ThongTin.Ten;
            cmbBoMon.Text = sinhvien.ThongTin.BoMon;
            if (sinhvien.ThongTin.GioiTinh == "Nam")
            {
                rdNam.Checked = true;
            }
            else
            {
                rdNu.Checked = true;
            }
            cmbLop.Text = sinhvien.ThongTin.Lop;
            dtpNgaySinh.Value = sinhvien.ThongTin.NgaySinh;
            txtQueQuan.Text = sinhvien.ThongTin.QueQuan;
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            sinhvien.ThongTin.Ten = txtTenSV.Text;
            sinhvien.ThongTin.Lop = cmbLop.Text;
            sinhvien.ThongTin.BoMon = cmbBoMon.Text;
            if (rdNam.Checked)
            {
                sinhvien.ThongTin.GioiTinh = "Nam";
            }
            else
            {
                sinhvien.ThongTin.GioiTinh = "Nữ";
            }
            sinhvien.ThongTin.NgaySinh = dtpNgaySinh.Value;
            sinhvien.ThongTin.QueQuan = txtQueQuan.Text;
            int check = sinhvien.Update();
            if (check > 0)
            {
                if (UpdateSuccess != null)
                {
                    InfoUpdateSinhVien info = new InfoUpdateSinhVien();
                    info.SinhVien = this.sinhvien;
                    UpdateSuccess(this, info);
                }
                this.Close();
            }
            else
            {
                MessageBox.Show("Có vấn đề khi sửa", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void frmSinhVien_Sua_Load(object sender, EventArgs e)
        {
            cmbBoMon.DataSource = bomon.ListName();
            btnCancel.Focus();
        }

        private void cmbBoMon_SelectedIndexChanged(object sender, EventArgs e)
        {
            cmbLop.DataSource = lop.ListName(cmbBoMon.Text);
        }

        private void txtTenSV_Leave(object sender, EventArgs e)
        {
            txtTenSV.Text = xString.Name(txtTenSV.Text);
        }

        private void label2_Click(object sender, EventArgs e)
        {

        }
    }
}
